package com.example.redisdemo1.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
public class Mycontroller {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    public String testString(){
        redisTemplate.opsForValue().set("key1","111");
        redisTemplate.opsForValue().set("key2","222");
        redisTemplate.opsForValue().set("key3","333");
        String value = redisTemplate.opsForValue().get("key1");
        System.out.println(value);
        return "OK";
    }
    @GetMapping("/testList")
    public String testList(){
        redisTemplate.opsForList().leftPush("key","a");
        redisTemplate.opsForList().rightPushAll("key","b","c");
        List<String> values = redisTemplate.opsForList().range("key",0 ,-1);
        System.out.println(values);
        redisTemplate.delete("key");
        return "OK";
    }
    @GetMapping("/testHash")
    public String testHash(){
        redisTemplate.opsForHash().put("key","name","wyz");
        String value = (String) redisTemplate.opsForHash().get("key","name");
        System.out.println(value);
        redisTemplate.opsForHash().delete("key","name");
        System.out.println(redisTemplate.opsForHash().hasKey("key", "name"));
        redisTemplate.delete("key");
        return "OK";
    }
    @GetMapping("/testSet")
    public String testSet(){
        redisTemplate.opsForSet().add("key","aaa","bbb","ccc");
        System.out.println(redisTemplate.opsForSet().isMember("key", "aaa"));
        System.out.println(redisTemplate.opsForSet().size("key"));
        redisTemplate.delete("key");
        return "OK";
    }
    @GetMapping("/testZSet")
    public String testZSet(){
        redisTemplate.opsForZSet().add("key","大飞",99);
        redisTemplate.opsForZSet().add("key","白糖",100);
        redisTemplate.opsForZSet().add("key","明月",98);
        Set<String> values = redisTemplate.opsForZSet().range("key",0,-1);
        System.out.println(values);
        long n = redisTemplate.opsForZSet().count("key",98,100);
        System.out.println(n);
        return "OK";
    }

}
